<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use app\api\service\MasterService;

/**
 * 师傅接口
 */
class Master extends Api
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 注册会员
     *
     * @ApiMethod (POST)
     * @param string $password 密码
     * @param string $confirmpw 确认密码
     * @param string $mobile   手机号
     * @param string $code     验证码
     */
    public function register()
    {
        $data = $this->request->post();
        $res = MasterService::register($data);
        return json($res);
    }

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $data = $this->request->post();
        $res = MasterService::login($data);
        return json($res);
    }



    /**
     * 重置密码
     *
     * @ApiMethod (POST)
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function resetpwd()
    {
        $data = $this->request->post();
        $res = MasterService::resetpwd($data);
        return json($res);
    }

    /**
     * 会员中心
     */
    public function index()
    {
        $data = $this->request->post();
        $res = MasterService::getUserInfo($data);
        return json($res);
    }

    // 上线下线开关
    public function online()
    {
        $data = $this->request->post();
        $res = MasterService::setOnline($data);
        return json($res);
    }

    /**
     * 积分明细
     */
    public function score()
    {
        $data = $this->request->post();
        $res = MasterService::getScoreList($data);
        return json($res);
    }

    /**
     * 我的优惠劵
     */
    public function coupon_list()
    {
        $data = $this->request->post();
        $res = MasterService::getCouponList($data);
        return json($res);
    }

    /**
     * 修改密码
     *
     * @ApiMethod (POST)
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function changepwd()
    {
        $data = $this->request->post();
        $res = MasterService::changepwd($data);
        return json($res);
    }

    // 信誉分说明
    public function credit_tips()
    {
        $data = $this->request->post();
        $res = MasterService::creditTips($data);
        return json($res);
    }

    // 购买信誉分
    public function buy_credit()
    {
        $data = $this->request->post();
        $res = MasterService::buyCredit($data);
        return json($res);
    }

    // 意见反馈
    public function feedback()
    {
        $data = $this->request->post();
        $res = MasterService::addFeedback($data);
        return json($res);
    }

    // 接单位置
    public function location()
    {
        $data = $this->request->post();
        $res = MasterService::editLocation($data);
        return json($res);
    }

    // 消息列表
    public function message()
    {
        $data = $this->request->post();
        $res = MasterService::getMessageList($data);
        return json($res);
    }


















    /**
     * 手机验证码登录
     *
     * @ApiMethod (POST)
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $data = $this->request->post();
        $res = MasterService::mobilelogin($data);
        return json($res);
    }

    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $avatar   头像地址
     * @param string $username 用户名
     * @param string $nickname 昵称
     * @param string $bio      个人简介
     */
    public function profile()
    {
        $data = $this->request->post();
        $res = MasterService::editUserProfile($data);
        return json($res);
    }

    /**
     * 注销账号
     */
    public function logoff()
    {
        $res = MasterService::logoff();
        return json($res);
    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        $res = MasterService::logout();
        return json($res);
    }









    /**
     * 修改邮箱
     *
     * @ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $captcha 验证码
     */
    public function changeemail()
    {
        $user = $this->auth->getUser();
        $email = $this->request->post('email');
        $captcha = $this->request->post('captcha');
        if (!$email || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Email already exists'));
        }
        $result = Ems::check($email, $captcha, 'changeemail');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->email = 1;
        $user->verification = $verification;
        $user->email = $email;
        $user->save();

        Ems::flush($email, 'changeemail');
        $this->success();
    }

    /**
     * 修改手机号
     *
     * @ApiMethod (POST)
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function changemobile()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Mobile already exists'));
        }
        $result = Sms::check($mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->mobile = 1;
        $user->verification = $verification;
        $user->mobile = $mobile;
        $user->save();

        Sms::flush($mobile, 'changemobile');
        $this->success();
    }

    /**
     * 第三方登录
     *
     * @ApiMethod (POST)
     * @param string $platform 平台名称
     * @param string $code     Code码
     */
    public function third()
    {
        $url = url('user/index');
        $platform = $this->request->post("platform");
        $code = $this->request->post("code");
        $config = get_addon_config('third');
        if (!$config || !isset($config[$platform])) {
            $this->error(__('Invalid parameters'));
        }
        $app = new \addons\third\library\Application($config);
        //通过code换access_token和绑定会员
        $result = $app->{$platform}->getUserInfo(['code' => $code]);
        if ($result) {
            $loginret = \addons\third\library\Service::connect($platform, $result);
            if ($loginret) {
                $data = [
                    'userinfo'  => $this->auth->getUserinfo(),
                    'thirdinfo' => $result
                ];
                $this->success(__('Logged in successful'), $data);
            }
        }
        $this->error(__('Operation failed'), $url);
    }


}
